﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using Forms=System.Windows.Forms;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Windows.Controls.Primitives;



namespace ZhwangTool
{
    /// <summary>
    /// PicEditorUserControl.xaml 的交互逻辑
    /// </summary>
    public partial class PicEditorUserControl : System.Windows.Controls.UserControl
    {
        public PicEditorUserControl()
        {
            InitializeComponent();
        }



        private Image movingObject;  // 记录当前被拖拽移动的图片
        private Point StartPosition; // 本次移动开始时的坐标点位置
        private Point EndPosition;   // 本次移动结束时的坐标点位置

        /// <summary>
        /// 按下鼠标左键，准备开始拖动图片
        /// </summary>
        /// <param name="p"></param>
        private void MouseLeftButtonDownCommand(object[] p)
        {
            object sender = p[0];
            MouseButtonEventArgs e = p[1] as MouseButtonEventArgs;
            Image img = sender as Image;

            movingObject = img;
            StartPosition = e.GetPosition(img);
        }

        /// <summary>
        /// 按住鼠标左键，拖动图片平移
        /// </summary>
        /// <param name="p"></param>
        private void MouseMoveCommand(object[] p)
        {
            object sender = p[0];
            MouseEventArgs e = p[1] as MouseEventArgs;
            Image img = sender as Image;

            if (e.LeftButton == MouseButtonState.Pressed && sender == movingObject)
            {
                EndPosition = e.GetPosition(img);

                TransformGroup tg = img.RenderTransform as TransformGroup;
                var tgnew = tg.CloneCurrentValue();
                if (tgnew != null)
                {
                    TranslateTransform tt = tgnew.Children[0] as TranslateTransform;

                    var X = EndPosition.X - StartPosition.X;
                    var Y = EndPosition.Y - StartPosition.Y;
                    tt.X += X;
                    tt.Y += Y;
                }

                // 重新给图像赋值Transform变换属性
                img.RenderTransform = tgnew;
            }
        }

        /// <summary>
        /// 鼠标左键弹起，结束图片的拖动
        /// </summary>
        /// <param name="p"></param>
        private void MouseLeftButtonUpCommand(object[] p)
        {
            object sender = p[0];
            MouseButtonEventArgs e = p[1] as MouseButtonEventArgs;
            Image img = sender as Image;
            movingObject = null;
        }




        private void Button1_Click(object sender, RoutedEventArgs e)
        {
            Forms.OpenFileDialog openFileDialog = new Forms.OpenFileDialog();

            // 设置文件类型过滤器，只允许用户选择图片文件  
            openFileDialog.Filter = "Image Files (*.jpg, *.jpeg, *.gif, *.bmp, *.png)|*.jpg;*.jpeg;*.gif;*.bmp;*.png";

            // 显示文件选择对话框，并获取用户选择的文件路径  
            if (openFileDialog.ShowDialog() == Forms.DialogResult.OK)
            {
                string selectedFilePath = openFileDialog.FileName;
                // 在这里使用用户选择的图片文件路径  
                MessageBox.Show("Selected image: " + selectedFilePath);
                
                image1.Source = new BitmapImage(new Uri(selectedFilePath));

                

            }


        }

        ScaleTransform st = new ScaleTransform();



        private void Thumb_DragDelta(object sender, System.Windows.Controls.Primitives.DragDeltaEventArgs e)
        {
            Thumb myThumb = (Thumb)sender;
            double nTop = Canvas.GetTop(myThumb) + e.VerticalChange;
            double nLeft = Canvas.GetLeft(myThumb) + e.HorizontalChange;

            Canvas.SetTop(myThumb, nTop);
            Canvas.SetLeft(myThumb, nLeft);
        }

        private void Image1_MouseWheel(object sender, MouseWheelEventArgs e)
        {
  
            Image img = sender as Image;

            TransformGroup tg = img.RenderTransform as TransformGroup;

            var tgnew = tg.CloneCurrentValue();
            
            Canvas cc = img.Parent as Canvas;

            if (tgnew != null)
            {
                ScaleTransform scale = tgnew.Children[1] as ScaleTransform;

                img.RenderTransformOrigin = new Point(0.5, 0.5);

                if (e.Delta > 0)
                {
                    scale.ScaleX += 0.1;
                    scale.ScaleY += 0.1;
                }
                else
                {
                    if (scale.ScaleX <=0.2)
                    {

                    }
                    else
                    {
                        scale.ScaleX -= 0.1;
                        scale.ScaleY -= 0.1;
                    }

                }
            }

            // 重新给图像赋值Transform变换属性
            img.RenderTransform = tgnew;
            
        }



        Point rotateStart;// 旋转图片前，按下鼠标右键时，鼠标相对于Part_ImageContainer的点
        /// <summary>
        /// 旋转图片功能
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void Button2_Click(object sender, RoutedEventArgs e)
        {
            //Part_ImageContainer.CaptureMouse();

            //mouseDown = true;

            //rotateStart = e.GetPosition(Part_ImageContainer);

            //// 需要注意的是：RotateTransfrom的CenterX和CenterY，始终是相对于原始坐标系（未经过变换的坐标系）的。
            //// 因此，设置CenterX和CenterY之后，需要点拉回到坐标原点
            //Point toImage = Part_ImageContainer.TranslatePoint(rotateStart, Part_Image);
            //Point center = group.Transform(toImage); // 将中心点转换到Part_Image现状的坐标系下
            //transer.X = (center.X - toImage.X * scaler.ScaleX);
            //transer.Y = (center.Y - toImage.Y * scaler.ScaleY);
            //rotater.CenterX = center.X;
            //rotater.CenterY = center.Y;




        }



        /// <summary>
        /// 图片左转
        /// </summary>
        /// <param name="img">被操作的前台Image控件</param>
        public void RotateLeft(Image img)
        {



            TransformGroup tg = img.RenderTransform as TransformGroup;
            var tgnew = tg.CloneCurrentValue();
            if (tgnew != null)
            {
                RotateTransform rt = tgnew.Children[2] as RotateTransform;
                img.RenderTransformOrigin = new Point(0.5, 0.5);
                rt.Angle -= 90;
            }

            // 重新给图像赋值Transform变换属性
            img.RenderTransform = tgnew;
        }

        /// <summary>
        /// 图片右转
        /// </summary>
        /// <param name="img">被操作的前台Image控件</param>
        public void RotateRight(Image img)
        {
            TransformGroup tg = img.RenderTransform as TransformGroup;
            var tgnew = tg.CloneCurrentValue();
            if (tgnew != null)
            {
                RotateTransform rt = tgnew.Children[2] as RotateTransform;
                img.RenderTransformOrigin = new Point(0.5, 0.5);
                rt.Angle += 90;
            }

            // 重新给图像赋值Transform变换属性
            img.RenderTransform = tgnew;
        }

        private void ButtonLeft_Click(object sender, RoutedEventArgs e)
        {
            RotateLeft(image1);
            
        }

        private void ButtonRight_Click(object sender, RoutedEventArgs e)
        {
            RotateRight(image1);
        }

        private void Button4_Click(object sender, RoutedEventArgs e)
        {
            //BitmapEncoder encoder = new JpegBitmapEncoder();

            //BitmapSource BS = (BitmapSource)image1.Source;

            //encoder.Frames.Add(BitmapFrame.Create(BS));

            //Forms.SaveFileDialog saveFileDialog = new Forms.SaveFileDialog();

            //Forms.DialogResult dialogResult = saveFileDialog.ShowDialog();

            //if (dialogResult==Forms.DialogResult.OK)
            //{
            //    string path = saveFileDialog.FileName;

            //    encoder.Save(new System.IO.FileStream(path, System.IO.FileMode.Create));


            //}

  






        }
        private double rotateAngle = 0.0;

        private void Button5_Click(object sender, RoutedEventArgs e)
        {
       

        }

        private bool isMouseLeftButtonDown;
        private Point previousMousePoint;
        private Cursor oldCursor;


        private void Image1_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            Image img = sender as Image;

            if (img == null) return;

            this.oldCursor = this.Cursor;

            this.Cursor = Cursors.SizeAll;

            img.CaptureMouse();

            this.isMouseLeftButtonDown = true;

            this.previousMousePoint = e.GetPosition(c);

        }


        private void Image1_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
        {
            Image img = sender as Image;

            if (img == null) return;

            this.Cursor = this.oldCursor;

            img.ReleaseMouseCapture();

            this.isMouseLeftButtonDown = false;
        }

        private void Image1_MouseMove(object sender, MouseEventArgs e)
        {
            Image image = sender as Image;

            if (image == null)
            {
                return;
            }

            if (this.isMouseLeftButtonDown && e.LeftButton == System.Windows.Input.MouseButtonState.Pressed)
            {
                this.DoImageMove(image, e.GetPosition(c));
            }
        }

        /// <summary>
        /// 暂时未用
        /// </summary>
        /// <param name="image"></param>
        /// <param name="position"></param>
        private void DoImageMove(Image img, Point position)
        {

            TransformGroup tg = img.RenderTransform as TransformGroup;

            var tgnew = tg.CloneCurrentValue();

            if (tgnew != null)
            {
                TranslateTransform transform = tgnew.Children[0] as TranslateTransform;

                transform.X += position.X - this.previousMousePoint.X;

                transform.Y += position.Y - this.previousMousePoint.Y;

                this.previousMousePoint = position;
                
            }

            // 重新给图像赋值Transform变换属性
            img.RenderTransform = tgnew;
            
        }


    }
}
